package com.beordie.control;

import com.beordie.model.Menu;
import com.beordie.model.Role;
import com.beordie.model.Users;
import com.beordie.service.IMenuService;
import com.beordie.service.IRoleService;
import com.beordie.service.impl.MenuServiceImpl;
import com.beordie.service.impl.RoleServiceImpl;
import com.beordie.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname RoleController
 * @Description 角色信息处理类
 * @Date 2021/6/16 22:46
 * @Created 30500
 */
@WebServlet(urlPatterns = "/power/role/roles")
public class RoleController extends HttpServlet {
    private IRoleService service = new RoleServiceImpl();
    private IMenuService menuService = new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 反射分发消息到对应的处理方法
        String methodString = req.getParameter("method");
        try {
            // 取出对应的方法
            Method method = this.getClass().getDeclaredMethod(methodString, HttpServletRequest.class, HttpServletResponse.class);
            try {
                method.invoke(this, req, resp);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 查询跳转
     * @author 30500
     * @date 2021/6/16 22:47
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void transitionAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 拿到所有的角色信息
        List<Role> roles = this.service.getAll();
        Users user = null;
        if (req.getAttribute("user") == null) {
            user = new Users();
        } else {
            user = (Users) req.getAttribute("user");
        }

        // 添加携带信息并转发页面
        req.setAttribute("roles", roles);
        req.setAttribute("user", user);
        req.getRequestDispatcher("/power/user/add.jsp").forward(req, resp);
    }

    /**
     * @description 数据列表显示页面
     * @author 30500
     * @date 2021/6/20 21:55
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 拿到起始页面页数
        String pageIndex = req.getParameter("index");
        int index = pageIndex == null ? 1 : Integer.parseInt(pageIndex);
        int total = 0;
        PageUtils pageUtils = new PageUtils();

        // 获取数据库的信息数据
        List<Role> roles = new ArrayList<>();
        total = service.getAllTotal();
        roles = service.getByLimit(index, pageUtils.getPageSize());

        // 设置页码工具信息
        pageUtils.setPageIndex(index);
        pageUtils.setTotal(total);
        pageUtils.setDataList(roles);

        req.setAttribute("pageUtils", pageUtils);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }

    /**
     * @description 查询二级菜单跳转
     * @author 30500
     * @date 2021/6/20 21:59
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 查询数据库的二级菜单信息
        List<Menu> menuList = menuService.getSecond();
        req.setAttribute("menuList", menuList);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }

    /**
     * @description 添加角色
     * @author 30500
     * @date 2021/6/20 22:25
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端返回的数据信息
        String roleName = req.getParameter("name");
        String[] menuID = req.getParameterValues("menuID");
        String roleState = req.getParameter("state");

        // 存储数据库并返回结果
        int result = service.add(roleName, roleState, menuID);
        if (result > 0) {
            // 成功跳转相应的界面
            resp.getWriter().print("<script>alert('添加成功');location.href='/power/role/roles?method=select'</script>");
        } else {
            // 失败跳转相应的界面并提示相应的失败信息
            resp.getWriter().print("<script>alert('添加失败');location.href='/power/role/roles?method=select'</script>");
        }
    }

    /**
     * @description 删除角色
     * @author 30500
     * @date 2021/6/21 21:45
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int roleId = Integer.parseInt(req.getParameter("id"));

        int result = service.delete(roleId);
        resp.sendRedirect("/power/role/roles?method=select");
    }

    /**
     * @description 更新角色
     * @author 30500
     * @date 2021/6/22 21:35
     * @type [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     */
    protected void transitionUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 查询数据库的二级菜单信息
        List<Menu> menuList = menuService.getSecond();

        // 取到数据库角色信息
        int roleId = Integer.parseInt(req.getParameter("id"));
        Role role = service.getById(roleId);

        // 取到角色所有的菜单 ID
        List<Integer> menuId = new ArrayList<>();
        for (Menu m1:role.getMenu())
            menuId.add(m1.getMenuId());


        req.setAttribute("menuId", menuId);
        req.setAttribute("role", role);
        req.setAttribute("menuList", menuList);

        req.getRequestDispatcher("/power/role/edit.jsp").forward(req, resp);

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端返回的数据信息
        String roleId = req.getParameter("id");
        String roleName = req.getParameter("name");
        String[] menuID = req.getParameterValues("menuID");
        String roleState = req.getParameter("state");

        // 更新并返回结果
        int result = service.update(roleId, roleName, roleState, menuID);
        resp.getWriter().print("<script>alert('更新成功');location.href='/power/role/roles?method=select'</script>");
    }
}
